import store from '@/store'


export default class BaseThree {
	constructor() {
    this.item = null;
    this.commonAttr = null;
    this.conAttr = null;
    this.parameters = null;
    this.store = store;
  }
  
  get componentMap() {
    return this.store.state.componentMap;
  }
  get controls() {
    return this.store.state.controls ? this.store.state.controls : this.store.state.wbComponents.controls;
  }
  get allPages() {
    return this.store.state.allPages ? this.store.state.allPages : this.store.state.components.controls;
  }
  get wbUserData() {
    return this.store.state.userInfo.wbUserData;
  }
  get userInfo() {
    return this.store.state.userInfo;
  }
  get interactionData() {
    return this.store.state.interactionData;
  }
  get frontData() {
    return this.store.state.frontData;
  }
  get formValues() {
    return this.store.state.formValues;
  }
  get globalIsPost() {
    return this.store.state.globalIsPost;
  }
  get pages() {
    return this.store.state.pages;
  }
  get pops() {
    return this.store.state.pops_;
  }
  get statePopId() {
    return this.store.state.popId;
  }
  get pageIndex() {
    return this.store.state.pageIndex;
  }
  get siteAttrs() {
    return this.store.state.attrs;
  }

  get timeObj() {
    return this.store.state.timeObj;
  }

  get appid() {
    return this.store.state.attrs.wb_appkey;
  }
  get uid() {
    return this.store.state.userInfo.uid;
  }
  get siteInfo() {
    return this.store.state.siteInfo;
  }


  initThree({ item, THREE, camera, renderer}){
    this.item = item;
    this.commonAttr = item.commonAttr;
    this.conAttr = item.conAttr;
    this.camera = camera;
    this.renderer = renderer;
    this.canvas = renderer.domElement;

    this.scene = new THREE.Scene();
    let parameters = this.item.interactionData && this.item.interactionData.injectJsClassObj.parameters;
    this.parameters = parameters;
    this.scene.background = new THREE.Color(parameters.bgColor);
    this.addToScene(THREE);
  }

  addToScene(THREE) {
  }

  addControls(THREE) {
    // Controls
    this.orbitControls = new THREE.OrbitControls(this.camera, this.canvas);
    this.orbitControls.enableDamping = true;
  }

  ticker () {
    // Update controls
    this.orbitControls.update();
    // Render
    this.renderer.render(this.scene, this.camera);
    // Call tick again on the next frame
    window.requestAnimationFrame(this.ticker.bind(this));
  }
 
  addAxesHelper(THREE) {
    const axesHelper = new THREE.AxesHelper(5);
    this.scene.add(axesHelper);
  }
  addCameraHelper(THREE) {
    const helper = new THREE.CameraHelper(this.camera);
    this.scene.add(helper);
  }
  
  // 动态加载css脚本
  loadStyleString(cssText) {
    var style = document.createElement("style");
    style.type = "text/css";
    try{
        // firefox、safari、chrome和Opera
        style.appendChild(document.createTextNode(cssText));
    }catch(ex) {
        // IE早期的浏览器 ,需要使用style元素的stylesheet属性的cssText属性
        style.styleSheet.cssText = cssText;
    }
    document.getElementsByTagName("head")[0].appendChild(style);
  }
  
  destroy() {
    this.item = null;
    this.commonAttr = null;
    this.conAttr = null;
    this.scene.destroy();
    this.scene = null;
    this.camera.destroy();
    this.camera = null;
    this.orbitControls.destroy();
    this.orbitControls = null;
    this.parameters = null;
  }
}